package com.vdf.sisconsolid.persistencemodule.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.vdf.sisconsolid.persistencemodule.converter.CellConverter;
import com.vdf.sisconsolid.persistencemodule.dto.CellDTO;
import com.vdf.sisconsolid.persistencemodule.dto.KeyValueBean;
import com.vdf.sisconsolid.persistencemodule.model.Address;
import com.vdf.sisconsolid.persistencemodule.model.Cell;
import com.vdf.sisconsolid.util.CacheSC;

public class CellDao extends BaseDao {

	public List<KeyValueBean> getCellList() {
		initTransaction();
		List<KeyValueBean> cellBeanList = CellConverter
				.toCellKeyValue(listAllWithoutTransaction("SELECT c FROM Cell c WHERE c.endDate IS NULL"));
		commitTransaction();

		return cellBeanList;
	}

	public void remove(Long idCell) {
		super.remove(Cell.class, idCell);

	}

	public void cancel(Long idCell) {
		initTransaction();
		try {
			Cell cell = getEm().find(Cell.class, idCell);
			cell.setEndDate(new Date());
			getEm().merge(cell);
			commitTransaction();
		} catch (Throwable e) {
			e.printStackTrace();
			rollbackTransaction();
		}
	}

	public Cell merge(CellDTO cDTO) {
		Cell cell = CellConverter.toEntity(cDTO);
		initTransaction();
		try {
			Address address = getEm().merge(cell.getAddress());
			cell.setAddress(address);
			cell = getEm().merge(cell);
			commitTransaction();
		} catch (Throwable e) {
			e.printStackTrace();
			rollbackTransaction();
		}
		return cell;
	}

	public List<CellDTO> getCellList(Map<String, Object> parameters) {

		StringBuilder query = new StringBuilder(
				"SELECT cell FROM Cell cell WHERE cell.endDate IS NULL ");

		if (parameters.size() > 0) {
			if (parameters.get("family") != null) {
				query.append(" and cell.family.id = :family ");
				parameters.put("family",
						((Integer) parameters.get("family")).longValue());
			}
			if (parameters.get("firstName") != null) {
				query.append(" and cell.leaderFirstName = :firstName ");
			}
			if (parameters.get("lastName") != null) {
				query.append(" and cell.leaderLastName = :lastName ");
			}
			if (parameters.get("idNeighborhood") != null) {
				query.append(" and cell.address.neighborhood.id = :idNeighborhood ");
				parameters.put("idNeighborhood",
						((Integer) parameters.get("idNeighborhood")).longValue());
				parameters.remove("idProvince");
				parameters.remove("idCity");
			}
		}
		super.initTransaction();
		List<CellDTO> cellDTOList = CellConverter.toDTOList((List<Cell>) super
				.listAllWithoutTransaction(query.toString(), parameters));
		super.commitTransaction();
		return cellDTOList;
	}

	public CellDTO find(Long idCell) {
		initTransaction();
		CellDTO cell = CellConverter.toDTO((Cell) getEm().find(Cell.class,
				idCell));
		commitTransaction();
		return cell;
	}

	public List<KeyValueBean> getDayList() {
		List<KeyValueBean> getDayList = new ArrayList<KeyValueBean>();
		for (Byte keyCS : CacheSC.getDays().keySet()) {
			KeyValueBean csBean = new KeyValueBean();
			csBean.setKey(keyCS.longValue());
			csBean.setValue(CacheSC.getDays().get(keyCS));
			getDayList.add(csBean);
		}
		return getDayList;

	}
}
